package com.example.hello.views.access_control;

import com.example.hello.*;
import com.example.hello.POJO.UserPOJO;
import com.example.hello.views.Wireframe1;
import com.example.hello.views.users.User;
import com.example.hello.views.users.Users;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

public class LogIn extends VerticalLayout implements View {

    public static final String NAME = "login";

    private HorizontalLayout layout = new HorizontalLayout();

    private BootstrapTabPanel panel = new BootstrapTabPanel(BootstrapTabPanel.Position.ABOVE);
    protected BootstrapTabLink logInLink = new BootstrapTabLink("Log In");
    protected BootstrapTabPane logInPane = new BootstrapTabPane();
    protected BootstrapTabLink signUpLink = new BootstrapTabLink("Sign Up");
    protected BootstrapTabPane signUpPane = new BootstrapTabPane();

    // LogIn fields
    private final TextField userName = new TextField("Username");
    private final PasswordField password = new PasswordField("Password");

    private final BootstrapButton logInSubmit = new BootstrapButton("Submit");

    // SignUp fields
    private final TextField firstName = new TextField("First Name");
    private final TextField lastName = new TextField("Last Name");
    private final TextField screenName = new TextField("Username");
    private final PasswordField password1 = new PasswordField("Password");
    private final PasswordField password2 = new PasswordField("Confirm Password");

    private final BootstrapButton signUpSubmit = new BootstrapButton("Submit");

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        logInLink.setActive(true);
        logInLink.getTabPane().setActive(true);
    }

    public LogIn() {
        setSizeFull();
        addComponent(layout);
        layout.setSizeFull();

        initTabPanel();
        initLogIn();
        initSignUp();

        layout.addComponent(panel);
        layout.setExpandRatio(panel, 1);
    }

    public void initTabPanel() {
        panel.setSizeFull();

        // link the tabLinks to the tabPanes
        logInLink.setTabPane(logInPane);
        logInPane.setHref("logIn");
        logInLink.setDataTarget("logIn");
        signUpLink.setTabPane(signUpPane);
        signUpPane.setHref("signUp");
        signUpLink.setDataTarget("signUp");

        panel.addTab(logInLink);
        panel.addTab(signUpLink);
        panel.addTab(logInPane);
        panel.addTab(signUpPane);

        logInLink.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                getUI().getNavigator().navigateTo(LogIn.NAME);
            }
        });
        signUpLink.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                getUI().getNavigator().navigateTo(SignUp.NAME);
            }
        });
    }

    public void initLogIn() {
        VerticalLayout logInLayout = new VerticalLayout();
        Listener logIn = new Listener() {
            @Override
            public void componentEvent(Event event) {
                logIn();
            }
        };
        logInSubmit.addListener(logIn);

        TextField.enterKeyHandler(logInSubmit, userName);
        TextField.enterKeyHandler(logInSubmit, password);

        logInLayout.addComponents(
                userName,
                password,
                new VSpacer(5),
                logInSubmit
        );
        logInPane.addWidget(logInLayout);
    }

    public void initSignUp() {
        VerticalLayout signUpLayout = new VerticalLayout();
        Listener signUp = new Listener() {
            @Override
            public void componentEvent(Event event) {
                signUp();
            }
        };
        signUpSubmit.addListener(signUp);

        TextField.enterKeyHandler(signUpSubmit, firstName);
        TextField.enterKeyHandler(signUpSubmit, lastName);
        TextField.enterKeyHandler(signUpSubmit, screenName);
        TextField.enterKeyHandler(signUpSubmit, password1);
        TextField.enterKeyHandler(signUpSubmit, password2);

        signUpLayout.addComponents(
                firstName,
                lastName,
                screenName,
                new VSpacer(),
                password1,
                password2,
                new VSpacer(5),
                signUpSubmit
        );
        signUpPane.addWidget(signUpLayout);
    }

    public boolean logIn() {
        UserPOJO user = User.GETUser(userName.getValue());

        if (user != null &&
                user.getUserName().equals(userName.getValue()) &&
                user.getPassword().equals(password.getValue())) {
            ((HelloUI) HelloUI.getCurrent()).setUser(user);
            ((HelloUI) HelloUI.getCurrent()).setLogInOutFlag(true);
            getUI().getSession().setAttribute("isLoggedIn", true);
            getUI().getNavigator().navigateTo(Wireframe1.NAME);
            return true;
        } else {
            addComponent(new BootstrapAlert("Invalid", BootstrapAlert.AlertType.ERROR), 0);
            userName.setValue("");
            password.setValue("");
            return false;
        }
    }

    public boolean signUp() {
        UserPOJO user = new UserPOJO(firstName.getValue(), lastName.getValue(),
                screenName.getValue(), password1.getValue());
        if (Users.addUser(user) && password1.getValue().equals(password2.getValue())) {
            ((HelloUI) HelloUI.getCurrent()).setUser(user);
            ((HelloUI) HelloUI.getCurrent()).setLogInOutFlag(true);
            getUI().getSession().setAttribute("isLoggedIn", true);
            getUI().getNavigator().navigateTo(Wireframe1.NAME);
            return true;
        } else {
            addComponent(new BootstrapAlert("Invalid", BootstrapAlert.AlertType.ERROR), 0);
            if (!password1.getValue().equals(password2.getValue())) {
                password1.setValue("");
                password2.setValue("");
            } else {
                screenName.setValue("");
            }
            return false;
        }
    }

}